package com.RPGDice;

import com.RPGDice.util.Settings;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.criticalritrpg.*;

public class SettingsActivity extends Activity implements OnClickListener,
		OnCheckedChangeListener {

	Button selectDices, saveSettings;
	EditText numberOfRolls, marginMin, marginMax;
	ToggleButton showSum, showSuccessFailure;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.settings);

		selectDices = (Button) findViewById(R.id.select_dices);
		saveSettings = (Button) findViewById(R.id.save_settings);

		selectDices.setOnClickListener(this);
		saveSettings.setOnClickListener(this);

		numberOfRolls = (EditText) findViewById(R.id.et_roll_num);
		marginMin = (EditText) findViewById(R.id.et_success_margin_min);
		marginMax = (EditText) findViewById(R.id.et_success_margin_max);

		showSum = (ToggleButton) findViewById(R.id.show_sum);
		showSuccessFailure = (ToggleButton) findViewById(R.id.show_success_failure);

		showSuccessFailure.setOnCheckedChangeListener(this);

		loadSettings();

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.select_dices:
			Intent mIntent = new Intent(getApplicationContext(),
					SelectDicesActivity.class);
			startActivity(mIntent);
			break;
		case R.id.save_settings:
			saveSettings();
			break;
		}
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.show_success_failure:
			showHideSuccessMargin();
			break;
		}
	}

	private void loadSettings() {
		marginMin.setText(String.valueOf(Settings.getSuccessMargin()[0]));
		marginMax.setText(String.valueOf(Settings.getSuccessMargin()[1]));
		numberOfRolls.setText(String.valueOf(Settings.getNumOfRolls()));
		showSuccessFailure.setChecked(Settings.getShowSuccess());
		showSum.setChecked(Settings.getShowSum());
		showHideSuccessMargin();

	}

	private void showHideSuccessMargin() {
		if (showSuccessFailure.isChecked()) {
			findViewById(R.id.tv_success_margin).setEnabled(true);
			marginMin.setEnabled(true);
			marginMax.setEnabled(true);
		} else {
			findViewById(R.id.tv_success_margin).setEnabled(false);
			marginMin.setEnabled(false);
			marginMax.setEnabled(false);
		}
	}

	private void saveSettings() {
		Settings.setNumOfRolls(getIntValue(numberOfRolls));
		Settings.setShowSum(showSum.isChecked());
		Settings.setShowSuccess(showSuccessFailure.isChecked());
		if (showSuccessFailure.isChecked()) {
			int lower = getIntValue(marginMin);
			int upper = getIntValue(marginMax);
			if (lower == -1 || upper == -1) {
				return;
			}
			switch (Settings.checkMargins(lower, upper)) {
			case 0:
				Settings.setSuccessMargin(lower, upper);
				break;
			case -1:
				Toast.makeText(this, R.string.upper_must_be_greater,
						Toast.LENGTH_LONG).show();
				return;
			case -2:
				Toast.makeText(this, R.string.lower_too_high, Toast.LENGTH_LONG)
						.show();
				return;
			case -3:
				Toast.makeText(this, R.string.upper_too_low, Toast.LENGTH_LONG)
						.show();
				return;
			}

		}
		finish();
	}

	private int getIntValue(EditText et) {
		int value = 0;
		try {
			value = Integer.parseInt(et.getText().toString());
		} catch (NumberFormatException e) {
			value = -1;
		}
		return value;
	}

}